package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description: 顺序打印，三个线程分别只能打印'A''B''C'，按顺序ABC打印十次
 */
public class ThreadDemo24 {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();
    private static Object locker3 = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for(int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        synchronized (locker2) {
                            System.out.print('A');
                            locker2.notify();
                        }
                        try {
                            locker1.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for(int i = 0; i < 10; i++) {
                    synchronized(locker2) {
                        synchronized (locker3) {
                            try {
                                locker2.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.print('B');
                            locker3.notify();
                        }
                    }
                }
            }
        };
        Thread t3 = new Thread() {
            @Override
            public void run() {
                for(int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        synchronized (locker1) {
                            try {
                                locker3.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.println('C');
                            locker1.notify();
                        }
                    }
                }
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }
}
